let rec power x n = match n with 
0 -> 1
| n when n < 0  -> invalid_arg "n doit etre positif"
| n -> x * power x (n-1);;

power 0 1;;

let rec application p x = match p with 
[]->0
| (e1,e2)::l -> e1 *(power x e2) +(application l x);;

application [(2,2);(2,3);(4,4)] 2;;

let rec add p q = match (p,q) with
([],l) | (l,[]) -> l
| ((coeff_p,exp_p)::l_p,(coeff_q,exp_q)::l_q) -> 
if exp_p = exp_q
    then if coeff_p - coeff_q = 0 
         then add l_p l_q 
         else (coeff_p + coeff_q,exp_p)::add l_p l_q
    else if exp_p > exp_q 
         then (coeff_q,exp_q)::add p l_q
         else (coeff_p,exp_p)::add l_p q;;

add [(1,2);(2,6);(7,8);(2,12)] [(1,4);(3,6);(1,9)];;

let rec soustract p q = match (p,q) with
([],l) | (l,[]) -> l
| ((coeff_p,exp_p)::l_p,(coeff_q,exp_q)::l_q) -> 
if exp_p = exp_q
    then if coeff_p = coeff_q 
         then add l_p l_q 
         else (coeff_p - coeff_q,exp_p)::soustract l_p l_q
    else if exp_p > exp_q 
         then (-coeff_q,exp_q)::soustract p l_q
         else (coeff_p,exp_p)::soustract l_p q;;

soustract [(1,2);(2,6);(7,8);(2,12)] [(1,4);(3,6);(1,9)];;

let rec deriv p = match p with 
[] -> []
| (e1,e2)::l -> (e2*e1,e2-1)::deriv l;;

deriv [(2,2);(2,3);(4,4)];;

